package com.yanze.schoolinformation.view.my.person;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.yanze.schoolinformation.R;
import com.yanze.schoolinformation.app.C;
import com.yanze.schoolinformation.base.BaseActivity;
import com.yanze.schoolinformation.databinding.ActivityIssueBinding;
import com.yanze.schoolinformation.model.bean.req.ReqIssue;
import com.yanze.schoolinformation.model.bean.resp.RespCurr;
import com.yanze.schoolinformation.model.http.DeforObserver;
import com.yanze.schoolinformation.model.http.reqo.DataReqo;
import com.yanze.schoolinformation.utils.DialogUtils;
import com.yanze.schoolinformation.utils.PerfectClickListener;
import com.yanze.schoolinformation.utils.ToastUtils;

import io.reactivex.disposables.Disposable;

public class IssueActivity extends BaseActivity<ActivityIssueBinding> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_issue);
        setTitle("问题反馈");
        showContentView();
        initView();
    }

    private void initView() {
        bindingView.btnCommit.setOnClickListener(new PerfectClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                String content = bindingView.etIssue.getText().toString();
                String contact = bindingView.etContact.getText().toString();
                if (content == null || contact.equals("")) {
                    ToastUtils.show(IssueActivity.this, "请输入您的意见");
                    return;
                }
                if (contact == null || contact.equals("")) {
                    ToastUtils.show(IssueActivity.this, "请输入您的联系方式");
                    return;
                }
                commitIssue(content,contact);
            }
        });
    }


    private void commitIssue(String content, String contact) {
        DialogUtils.showDialog(this,"正在提交...");
        ReqIssue issue = new ReqIssue(content,contact);
        DataReqo.getInstant().fackIssue(C.url.API_School, issue)
                .subscribe(new DeforObserver<RespCurr>() {
                    @Override
                    public void _onSubscribe(Disposable disposable) {
                        addSubscription(disposable);
                    }

                    @Override
                    public void _onNext(RespCurr entity) {
                        DialogUtils.dismissDialog();
                        if (entity.getStatus() == 200) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(IssueActivity.this);
                            builder.setTitle("提示：");
                            builder.setMessage("感谢您的反馈，反正我也不看~");
                            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    finish();
                                }
                            });
                            builder.show();
                        }else{
                            ToastUtils.show(entity.getMsg());
                        }
                    }

                    @Override
                    public void _onError(String msg) {
                        DialogUtils.dismissDialog();
                        ToastUtils.show(msg);
                    }
                });
    }

    /**
     * 入口
     */
    public static void start(Context context) {
        context.startActivity(new Intent(context,IssueActivity.class));
    }
}
